#!/bin/sh
#
# USB WD My Book Fuel Gauge "cpu meter" script
# Copyright (C) 2007 Jonathan Krishnanantham, AsGF2MX@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, version 2.
#
# How it works:
# This script takes in the disk path (/dev/sdX<optional partition no.>)
# on which the leds are to be manipulated. It then obtains the CPU usage
# from uptime for the last 1,5 and 15 minutes and displays one of these
# three according to the options. 
#

# Usage and help statements
usage="Usage: wdcpumeter [-d disk] [-t 1 OR 2 OR 3] [-hq]" ;
help="Valid options: \n
-d [disk path] - this path must be of the form /dev/sdX \n
-h - prints this message \n
-q - quiet mode \n
-t [1,2 OR 3] - CPU load average 1=15 min,2=5 min OR 3=1 min" ;

# Quiet mode, default = off
quiet=false ;

# Error statements
e_exitstatus="Info: wdsetleds exited with code : " ;
e_invalidtime="Error: Invalid 'time' specified.";
e_multidisk="Error: Multiple disks specified." ;
e_multitime="Error: Multiple 'time' specified." ;
e_nodisk="Error: No disk specified." ;
e_setledfailed="Error: Failed to set leds. Run wdsetleds for further details." ;
e_specifyone="Specify only one value." ;

# Option error code
E_OPTERROR=65 ;

# If no arguments were provided,return usage and set exit code
if [ $# -eq 0 ] ;
then
  echo $usage ;
  exit $E_OPTERROR ;
fi

# Get all the options
while getopts "d:t:qh" options ;
do
  case $options in
    d ) if [ -z "$disk" ] ;
        then
          disk=$OPTARG ;
        else
          echo $e_multidisk $e_specifyone ;
          exit $E_OPTERROR ;
        fi ;;
    t ) if [ -z "$time" ] ;
        then
          time=$OPTARG ;
        else
          echo $e_multitime $e_specifyone ;
          exit $E_OPTERROR ;
        fi ;;
    q ) quiet=true      ;;
    h ) echo $usage     ;
        echo -e $help   ;
        exit 0          ;;
    [?] ) echo $usage   ;
          exit 1        ;;
    * ) echo $usage     ;
        exit 1          ;;
  esac
done
             
# If no disk was specified, quit.
if [ -z "$disk" ] ;
then
  if [ $quiet = false ] ;
  then
    echo $e_nodisk ;
  fi
  exit $E_OPTERROR ;
fi

# If no time was specified, use the 15 min option.
if [ -z "$time" ] ;
then
  time=1 ;
fi

# Determine the CPU load as per the option
# CPU load in last 15 minutes
if [ $time -eq 1 ] ;
then
  cpuload="$(uptime|awk '{ print $10 }'|sed s/,//g )" ;
# CPU load in last 5 minutes
elif [ $time -eq 2 ] ;
then
  cpuload="$(uptime | awk '{ print $9 }' | sed s/,//g )" ;
# CPU load in last minute
elif [ $time -eq 3 ] ;
then
  cpuload="$(uptime | awk '{ print $8 }' | sed s/,//g )" ;
# If it was none of the above then the number is invalid.
else
  if [ $quiet = false ] ;
  then
    echo $e_invalidtime ;
  fi
  exit $E_OPTERROR ;
fi

# Calculate the percentage in use
utilized=$(echo "scale=0; 100*$cpuload/5" | bc) ;

# Determine the led value to be set based on usage percentage
# 0-3 %
if [ $utilized -ge 0 ] && [ $utilized -lt 4 ] ;
then
  ledvalue=0 ;
#  4-20 %
elif [ $utilized -ge 4 ] && [ $utilized -lt 20 ] ;
then
  ledvalue=1 ;
#  20-35 %
elif [ $utilized -ge 20 ] && [ $utilized -lt 36 ] ;
then
  ledvalue=3 ;
#  36-51 %
elif [ $utilized -ge 36 ] && [ $utilized -lt 52 ] ;
then
  ledvalue=7 ;
#  52-67 %
elif [ $utilized -ge 52 ] && [ $utilized -lt 68 ] ;
then
  ledvalue=15 ;
#  68-83 %
elif [ $utilized -ge 68 ] && [ $utilized -lt 84 ] ;
then
  ledvalue=31 ;
#  84-100 %
elif [ $utilized -ge 84 ] && [ $utilized -le 100 ] ;
then
  ledvalue=63 ;
fi

# Now call the setleds script
wdsetleds -d $disk -s $ledvalue -q  ;

# Store the wdsetleds exit status
wdsetledstatus=$? ;

# If the leds failed to be set.
if [ $wdsetledstatus -ne 0 ] ;
  then
    # Display message if quiet is unset
    if [ $quiet = false ] ;
    then
      echo $e_setledfailed ;
      echo $e_exitstatus $wdsetledstatus ;
    fi
fi

# Return wdsetleds exit status.
exit $wdsetledstatus ;
